package com.banzhi.shop.fragment;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.activity.GroupingDetailsActivity;
import com.banzhi.shop.data.GroupingDetailsBean;
import com.banzhi.shop.model.CallBack.GroupingDetailsCallback;
import com.banzhi.shop.model.Impl.GroupingDetailsImpl;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONObject;

/**
 * Created by Administrator on 2018/7/19.
 */
//拼团商品介绍
public class GoodsIntroductionFragment extends Fragment implements GroupingDetailsCallback {

    private View view;
    private GroupingDetailsImpl mModel;
    private Activity activity;
    private String GoodID;
    private GroupingDetailsBean mGroupingDetailsBean;
    private WebView webView;
    private FrameLayout mFrameLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_goods_introduction,container,false);

        initView();
        mModel = new GroupingDetailsImpl();
        activity = getActivity();
        if (activity instanceof GroupingDetailsActivity) {
            GoodID = ((GroupingDetailsActivity) activity).getGoodID();
        }
        mModel.getGroupingDetailsData(GoodID,this);
        return view;
    }

    @Override
    public void onPause() {
        webView.onPause();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        webView.destroy();
        super.onDestroy();
    }

    private MyWebChromeClient mMyWebChromeClient;

    private void initView() {
        webView = (WebView)view.findViewById(R.id.webview);
        mFrameLayout = (FrameLayout) view.findViewById(R.id.mFrameLayout);
        getActivity().getWindow().setFlags(//强制打开GPU渲染
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        WebSettings settings = webView.getSettings();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        //允许与js交互
        settings.setJavaScriptEnabled(true);
        //设置webview的插件转状态
        settings.setPluginState(WebSettings.PluginState.ON);
        //webview的缓存模式
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        //自动加载图片
        settings.setLoadsImagesAutomatically(true);
        settings.setBuiltInZoomControls(true);
        //自动缩放
        settings.setSupportZoom(true);

        mMyWebChromeClient = new MyWebChromeClient();
        webView.setWebChromeClient(mMyWebChromeClient);
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
    }

    @Override
    public void ongetGroupingDetailsDataSuccess(Object object) {
        mGroupingDetailsBean = (GroupingDetailsBean) object;
        showWebView();
    }

    private void showWebView() {
        String str = mGroupingDetailsBean.getContent();

        webView.loadDataWithBaseURL(null, str, "text/html", "UTF-8", null);
        LogUtils.d("链接", str);

    }

    @Override
    public void ongetGroupingDetailsDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));

    }

    private class MyWebChromeClient extends WebChromeClient {
        private View mCustomView;
        private CustomViewCallback mCustomViewCallback;

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            super.onShowCustomView(view, callback);
            if (mCustomView != null) {
                callback.onCustomViewHidden();
                return;
            }
            mCustomView = view;
            mFrameLayout.addView(mCustomView);
            mCustomViewCallback = callback;
            webView.setVisibility(View.GONE);
            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

        public void onHideCustomView() {
            webView.setVisibility(View.VISIBLE);
            if (mCustomView == null) {
                return;
            }
            mCustomView.setVisibility(View.GONE);
            mFrameLayout.removeView(mCustomView);
            mCustomViewCallback.onCustomViewHidden();
            mCustomView = null;
            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            super.onHideCustomView();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
        switch (config.orientation) {
            case Configuration.ORIENTATION_LANDSCAPE:
                getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                break;
            case Configuration.ORIENTATION_PORTRAIT:
                getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                break;
        }
    }
}
